<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Plugins_RuleChecker extends Gio_Core_Plugin
{
	private $_coreModule = 'core';
	
	public function run()
	{
		$controller = Gio_Core_Controller::getIntance();
		$view = Gio_Core_View::getInstance();
		
		$configs = Gio_Core_Config_Xml::getConfig('web');
		if ($view->APP_TEMPLATE != (string)$configs->admin->template) {
			return;
		}
		
		$moduleId = $controller->getModuleName();
		/**
		 * Check Module Installed
		 */
		if ($moduleId != $this->_coreModule && null == Modules_Core_Services_Module::getById($moduleId)) {
			Modules_Core_Services_Exception::error('PERMISSION_DENY');
			return;
		}
		
		$ruleChecker = Gio_Core_Acl::getIntance();
		$actionId = $controller->getActionName();
		$controllerId = $controller->getControllerName();
		
		if ($ruleChecker->isAllowed($actionId, $controllerId, $moduleId) === false) {
			Modules_Core_Services_Exception::error('PERMISSION_DENY');
		}		
	}
}